home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / SocketClasses / Chat / Chatserver.m < prev    next >
Encoding:
Text File  |  1992-07-23  |  12.2 KB  |  272 lines

  1. /***************************************************************************
  2. *                                                                          *
  3. * Chatserver.m                                                             *
  4. * Copyright 1992 by Nik A Gervae                                           *
  5. *                                                                          *
  6. * Part of an example using the Objective-C classes (SktSocketManager,      *
  7. * SktSocket, and SktSocketUser) which implement a convenient interface to  *
  8. * Berkeley stream sockets under NeXTSTEP(r).  See the accompanying class   *
  9. * specifications (files with a .rtf or .spec suffix) and the sources for   *
  10. * further information.                                                     *
  11. *                                                                          *
  12. * NeXTSTEP is a registered trademark of NeXT Computer, Inc.                *
  13. *                                                                          *
  14. ****************************************************************************
  15. *                                                                          *
  16. * LICENSE                                                                  *
  17. *                                                                          *
  18. * This program is free software; you can redistribute it and/or modify     *
  19. * it under the terms of the GNU General Public License as published by     *
  20. * the Free Software Foundation.                                            *
  21. *                                                                          *
  22. * The program and this makefile are distributed in the hope that it will   *
  23. * be useful, but are provided "AS IS" AND WITHOUT ANY WARRANTY; without    *
  24. * any express or implied warranty of MERCHANTABILITY or FITNESS FOR A      *
  25. * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
  26. * Any use or distribution of the program and documentation must include    *
  27. * appropriate copyrights to acknowledge Nik A. Gervae and the Free         *
  28. * Software Foundation, Inc.                                                *
  29. *                                                                          *
  30. * You should have received a copy of the GNU General Public License        *
  31. * along with this program; if not, write to the Free Software              *
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
  33. *                                                                          *
  34. ****************************************************************************
  35. *                                                                          *
  36. * VERSION HISTORY                                                          *
  37. *                                                                          *
  38. * Version numbers are simply dates in the form YYYYMMDD.  These represent  *
  39. * the date that version was finished.  Only significantly changed versions *
  40. * are reported here, or those versions requiring explanation of changes.   *
  41. * There may be many interim stages between dated versions.                 *
  42. *                                                                          *
  43. * DateVersion Primary Author  Notes                                        *
  44. * ----------- --------------- -------------------------------------------- *
  45. * 19920327    Nik A Gervae    First released version                       *
  46. *                                                                          *
  47. ***************************************************************************/
  48.  
  49. #import <stdio.h>
  50. #import <string.h>
  51.  
  52. #import <objc/Object.h>
  53. #import <objc/List.h>
  54.  
  55. #import "SktSocketManager.h"
  56. #import "SktSocket.h"
  57. #import "Chatserver.h"
  58. #import "Guest.h"
  59.  
  60. /***************************************************************************
  61. *                                                                          *
  62. * These are the constant strings used.  Feel free to translate them into   *
  63. * your favorite language.  Do be sure to keep all the % directives in      *
  64. * place, or change the code that accesses these strings.                   *
  65. *                                                                          *
  66. ***************************************************************************/
  67. #define STR_RunningOnAddresses  "Running on %s at Internet addresses:"
  68. #define STR_AnAddress           " %s"
  69. #define STR_InetServicePort     "Internet service port number: %d\n"
  70. #define STR_EndOfAddresses      "\n"
  71.  
  72.  
  73.  
  74. @implementation Chatserver
  75.  
  76. /***************************************************************************
  77. *                                                                          *
  78. * initWithInetPort:                                                        *
  79. *                                                                          *
  80. * Creates a SocketMgr object and initializes the guestList. Also logs      *
  81. * the host, address and port number the server is running on.              *
  82. *                                                                          *
  83. ***************************************************************************/
  84. - initWithInetPort:(int)inetPort
  85. {
  86.   char **addresses;
  87.   int    i;
  88.  
  89.  /*
  90.   * Get a manager, bomb if you can't.
  91.   */
  92.   socketMgr = [[SktSocketManager alloc] initPort:inetPort logfile:stderr
  93.                fdCapacity:10 userClass:[Guest class]];
  94.   if (!socketMgr) return [self free];
  95.  
  96.  /*
  97.   * Try to print out all the internet addresses for this machine.
  98.   */
  99.   if (NULL != (addresses = [socketMgr getInetAddresses])) {
  100.     [self log:STR_RunningOnAddresses, [socketMgr hostname]];
  101.     for (i = 0; NULL != addresses[i]; i++) {
  102.       [self log:STR_AnAddress, addresses[i]];
  103.     }
  104.     [self log:STR_EndOfAddresses];
  105.     free(addresses);
  106.   }
  107.  
  108.  /*
  109.   * Print out the port number.
  110.   */
  111.   [self log:STR_InetServicePort, [socketMgr servicePort]];
  112.  
  113.  /*
  114.   * A new guest list, please.
  115.   */
  116.   guestList = [[List alloc] init];
  117.  
  118.   return self;
  119. }
  120.  
  121. /***************************************************************************
  122. *                                                                          *
  123. * run                                                                      *
  124. *                                                                          *
  125. * Run forever. The -stop message never gets sent.  Excercise for the       *
  126. * reader.                                                                  *
  127. *                                                                          *
  128. ***************************************************************************/
  129. - run
  130. {
  131.   running = YES;
  132.   while(running) {
  133.     usleep(250000);
  134.     [self update];
  135.   }
  136.   return self;
  137. }
  138.  
  139. /***************************************************************************
  140. *                                                                          *
  141. * update                                                                   *
  142. *                                                                          *
  143. * socketMgr needs this to work. Tell socketMgr to update, and do the same  *
  144. * for each guest.                                                          *
  145. *                                                                          *
  146. ***************************************************************************/
  147. - update
  148. {
  149.   [socketMgr update];
  150.   [guestList makeObjectsPerform:@selector(update)];
  151.   return self;
  152. }
  153.  
  154. /***************************************************************************
  155. *                                                                          *
  156. * guestDidInit:                                                            *
  157. *                                                                          *
  158. * Any newly created Guest sends this. This just saves it in the guest list *
  159. * for further reference.                                                   *
  160. *                                                                          *
  161. ***************************************************************************/
  162. - guestDidInit:aGuest
  163. {
  164.   [guestList addObject:aGuest];
  165.   return self;
  166. }
  167.  
  168. /***************************************************************************
  169. *                                                                          *
  170. * guestWillFree:                                                           *
  171. *                                                                          *
  172. * A guest send this as its about to die.  This just removes it from the    *
  173. * guest list                                                               *
  174. ***************************************************************************/
  175. - guestWillFree:aGuest
  176. {
  177.   [guestList removeObject:aGuest];
  178.   return self;
  179. }
  180.  
  181. /***************************************************************************
  182. *                                                                          *
  183. * log:                                                                     *
  184. *                                                                          *
  185. * Write out a printf() style argument list to stderr.                      *
  186. *                                                                          *
  187. ***************************************************************************/
  188. - log:(const char *)format, ...
  189. {
  190.   va_list param_list;
  191.  
  192.   va_start(param_list, format);
  193.   vfprintf(stderr, format, param_list);
  194.   fflush(stderr);
  195.   va_end(param_list);
  196.   return self;
  197. }
  198.  
  199. /***************************************************************************
  200. *                                                                          *
  201. * announce:                                                                *
  202. *                                                                          *
  203. * Tell something to every guest.                                           *
  204. *                                                                          *
  205. ***************************************************************************/
  206. - announce:(const char *)announcement
  207. {
  208.   int numGuests;
  209.   int i;
  210.  
  211.   numGuests = [guestList count];
  212.   for (i = 0; i < numGuests; i++) {
  213.     [[guestList objectAt:i] queueOutputString:announcement];
  214.   }
  215.  
  216.   return self;
  217. }
  218.  
  219. /***************************************************************************
  220. *                                                                          *
  221. * announce:except:                                                         *
  222. *                                                                          *
  223. * Tell something to every guest except the one mentioned.                  *
  224. *                                                                          *
  225. ***************************************************************************/
  226. - announce:(const char *)announcement except:notMe
  227. {
  228.   int numGuests;
  229.   int i;
  230.   id  currentGuest;
  231.  
  232.   numGuests = [guestList count];
  233.   for (i = 0; i < numGuests; i++) {
  234.     currentGuest = [guestList objectAt:i];
  235.     if (currentGuest != notMe) [currentGuest queueOutputString:announcement];
  236.   }
  237.  
  238.   return self;
  239. }
  240.  
  241. /***************************************************************************
  242. *                                                                          *
  243. * stop                                                                     *
  244. *                                                                          *
  245. * Should be reasonably clear.  Except that it never gets sent!  :-)        *
  246. *                                                                          *
  247. ***************************************************************************/
  248. - stop
  249. {
  250.   running = NO;
  251.   return self;
  252. }
  253.  
  254. /***************************************************************************
  255. *                                                                          *
  256. * shutdown                                                                 *
  257. *                                                                          *
  258. * If there were more complex structures/objects, you might want to save    *
  259. * them to disk or free them all here.                                      *
  260. *                                                                          *
  261. ***************************************************************************/
  262. - shutdown
  263. {
  264.   return [self free];
  265. }
  266.  
  267.  
  268. @end /*implementation Chatserver*/
  269.  
  270. /***************************************************************************
  271. ***************************************************************************/
  272.